package com.hequ.merchant.banner.activity;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.hequ.merchant.banner.R;
import com.hequ.merchant.banner.api.ModuleAddRequest;
import com.hequ.merchant.banner.api.OnRequestListener;
import com.hequ.merchant.banner.api.SimpleRequestListener;
import com.hequ.merchant.banner.entity.Module;
import com.hequ.merchant.banner.util.Toasts;

import java.util.HashMap;
import java.util.Map;

import static com.hequ.merchant.banner.handler.BannerJsonResponseHandler.JSON_KEY_ID;
import static com.hequ.merchant.banner.handler.BannerJsonResponseHandler.JSON_KEY_MEMO;
import static com.hequ.merchant.banner.handler.BannerJsonResponseHandler.JSON_KEY_NAME;
/**
 * Created by 柯尊诚 on 2015/10/26.
 * kzc
 */
public class AddModuleActivity extends Activity {

    private EditText nameField;
    private EditText memoField;
    private Button subumitBtn;

    private String nameValues;
    private String memoValues;
    private String moduleId;


    public static boolean isChange = false;
    private RequestQueue requestQueue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.modules_add);

        init();

        requestQueue = Volley.newRequestQueue(this);


        if (isChange) {
            changeInfo();
        }
    }

    /**
     * 修改信息
     */
    private void changeInfo() {
        Module module = (Module) getIntent().getSerializableExtra("module");
        nameField.setText(module.getName());
        memoField.setText(module.getMemo());
        moduleId = module.getId();
    }

    private void init() {
        nameField = (EditText) findViewById(R.id.et_m_name);
        memoField = (EditText) findViewById(R.id.et_m_memo);
        subumitBtn = (Button) findViewById(R.id.btn_m_submit);


        subumitBtn.setOnClickListener(onClicksubumit);
    }

    /**
     * 获取表单数据
     */
    public void getValues() {
        nameValues = nameField.getText().toString().trim();
        memoValues = memoField.getText().toString().trim();
    }


    /**
     * 提交数据
     */
    private View.OnClickListener onClicksubumit = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            requestQueue.start();
            requestQueue.add(new ModuleAddRequest(moduleAddListener));
        }
    };

    /**
     * 网络请求，提交数据
     */
    private OnRequestListener<String> moduleAddListener = new SimpleRequestListener<String>() {
        @Override
        public void onSuccess(String data) {
            onNetSuccess(data);
        }

        @Override
        public void onFailed() {
            onNetFailed();
        }

        @Override
        public Map<String, String> getParams() {
            Map<String, String> params = super.getParams();
            params.putAll(onNetGetParams());
            return params;
        }
    };

    private Map<String, String> onNetGetParams() {
        Map<String, String> params = new HashMap<>();
        getValues();

        params.put(JSON_KEY_NAME, nameValues);
        params.put(JSON_KEY_MEMO, memoValues);
        if (!TextUtils.isEmpty(moduleId)) {
            params.put(JSON_KEY_ID, moduleId);
        }

        return params;
    }


    private void onNetSuccess(String data) {
        Toasts.makeText("提交成功！");
        finish();
    }

    private void onNetFailed() {
        Toasts.makeText("提交失败！");
    }



}
